package starcoast.bocai.com.starcoast.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;


import java.lang.ref.WeakReference;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import starcoast.bocai.com.starcoast.R;
import starcoast.bocai.com.starcoast.pressent.PhotoPressent;
import starcoast.bocai.com.starcoast.util.BitmapUtil;

/**
 * Created by admin on 2016/4/22.
 */
public class PostPhotoAdapter extends BaseAdapter {
    private Context context;
    ArrayList<String> photos;
    PhotoPressent pressent;
    public PostPhotoAdapter(Context context, ArrayList<String> photos, PhotoPressent pressent) {
        this.context = context;
        this.photos=photos;
        this.pressent=pressent;
    }
    @Override
    public int getCount() {
        int count=photos.size();
        if(photos.size()<3){
            count= photos.size()+1;
        }else{
            count = 3;
        }
        return count;
    }

    @Override
    public Object getItem(int i) {
        return photos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.photo_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        if(photos.size()<3){

            if (position == photos.size()){
                viewHolder.ivRegistPhoto.setImageResource(R.mipmap.img_add);
                viewHolder.ivRemovePhoto.setVisibility(View.GONE);
                viewHolder.ivRegistPhoto.setOnClickListener(new MyOnclicklitener(position));
            }else{
                viewHolder.ivRegistPhoto.setImageBitmap(BitmapUtil.compressBitmap(photos.get(position)));
                viewHolder.ivRegistPhoto.setClickable(false);
                viewHolder.ivRemovePhoto.setVisibility(View.VISIBLE);
                viewHolder.ivRemovePhoto.setOnClickListener(new MyOnclicklitener(position));
            }
        }else{
            Log.e("photos",photos.get(position)+"");
            viewHolder.ivRegistPhoto.setImageBitmap(BitmapUtil.compressBitmap(photos.get(position)));
            viewHolder.ivRegistPhoto.setClickable(false);
            viewHolder.ivRemovePhoto.setVisibility(View.VISIBLE);
            viewHolder.ivRemovePhoto.setOnClickListener(new MyOnclicklitener(position));
        }
        return convertView;
    }
    class MyOnclicklitener implements View.OnClickListener{
        int position;
        public MyOnclicklitener(int position){
            this.position=position;
        }
        @Override
        public void onClick(View v) {
            switch (v.getId())
            {
                case R.id.iv_regist_photo:
                    pressent.startCamera();
                    break;
                case R.id.iv_regist_removephoto:
                    photos.remove(position);
                    notifyDataSetChanged();
                    break;
            }
        }
    }
    class ViewHolder {
        @Bind(R.id.iv_regist_photo)
        ImageView ivRegistPhoto;
        @Bind(R.id.iv_regist_removephoto)
        ImageView ivRemovePhoto;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public Bitmap convertToBitmap(String path, int w, int h) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        // 设置为ture只获取图片大小
        opts.inJustDecodeBounds = true;
        opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
        // 返回为空
        BitmapFactory.decodeFile(path, opts);
        int width = opts.outWidth;
        int height = opts.outHeight;
        float scaleWidth = 0.f, scaleHeight = 0.f;
        if (width > w || height > h) {
            // 缩放
            scaleWidth = ((float) width) / w;
            scaleHeight = ((float) height) / h;
        }
        opts.inJustDecodeBounds = false;
        float scale = Math.max(scaleWidth, scaleHeight);
        opts.inSampleSize = (int) scale;
        WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
        return Bitmap.createScaledBitmap(weak.get(), w, h, true);
    }
}
